package com.lzp.badminton.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzp.badminton.model.Clubmember;
import com.lzp.badminton.mapper.ClubmemberMapper;
import com.lzp.badminton.service.IClubmemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzp.badminton.util.DateUtils;
import com.lzp.badminton.vo.ActivityClubJoin;
import com.lzp.badminton.vo.ClubUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2022-02-11
 */
@Service
public class ClubmemberServiceImpl extends ServiceImpl<ClubmemberMapper, Clubmember> implements IClubmemberService {
    @Autowired
    private ClubmemberMapper clubmemberMapper;

    @Override
    public Page<ClubUser> getClubMemberById(Page<ClubUser> page, int clubId) {
        return page.setRecords(this.baseMapper.getClubMemberById(page, clubId));
    }

    // 获取用户是否参加了俱乐部
    public int getClubUserIn(int clubId, String userId) {
        Map c = new HashMap();
        c.put("to_club_id", clubId);
        c.put("from_user_id", userId);

        List clist = clubmemberMapper.selectByMap(c);

        if (clist == null || clist.size() == 0) {
            return  0;
        } else {
            return 1;
        }
    }

    // 用户加入俱乐部
    public int userToClub (int clubId, String userId) {
        Clubmember cm = new Clubmember();
        cm.setFromUserId(userId);
        cm.setStatus(1);
        cm.setToClubId(clubId);
        String date = new DateUtils().getCurrentDate();
        cm.setJoinTime(date);

        int result = clubmemberMapper.insert(cm);
        return result;
    }

    // 用户退出俱乐部
    public int userLeaveClub (int clubId, String userId) {
        Map map = new HashMap();
        map.put("to_club_id", clubId);
        map.put("from_user_id", userId);

        int result = clubmemberMapper.deleteByMap(map);
        return result;
    }

    // 模糊查询俱乐部成员
    public Page<ClubUser> getClubMemberInfo (Page<ClubUser> page, int clubId, String nickname) {
        return page.setRecords(this.baseMapper.getClubMemberInfo(page, clubId, nickname));
    }
}
